package com.mhkj.base.earlyWarning.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mhkj.base.earlyWarning.entity.EarlyWarning;
import com.mhkj.base.earlyWarning.service.EarlyWarningService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.common.componet.ResponseMessage;
import com.mics.core.system.base.controller.BaseController;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;
import com.mics.core.system.rbac.service.DictionaryService;

/**
 * 预警
 */
@Controller
@RequestMapping("/EarlyWarning")
public class EarlyWarningController extends BaseController<EarlyWarning> {
	@Autowired
	private EarlyWarningService EarlyWarningService;
	@Autowired
	private OrgService orgService;
	@Autowired
	private DictionaryService dictionaryService;
	
	@RequestMapping(value="/listByPage", method=RequestMethod.POST)
	@ResponseBody
	public Pagination<EarlyWarning> listByPage( EarlyWarning EarlyWarning, int pageIndex, int pageSize ) {
		Pagination<EarlyWarning> pagination = EarlyWarningService.listByPage(EarlyWarning,pageIndex, pageSize);
		List<EarlyWarning> data = pagination.getData();
		for (EarlyWarning earlyWarning2 : data) {
			Employee employeeInfo = orgService.employeeInfo(earlyWarning2.getCreateBy());
			earlyWarning2.setCreateName(employeeInfo.getName());
		}
		return pagination;
	}

	/**
	 * 保存
	 */
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage save(EarlyWarning EarlyWarning) {
		String message = EarlyWarningService.save(EarlyWarning);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 修改
	 * 
	 */
	@RequestMapping(value="/edit", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage edit(EarlyWarning EarlyWarning) {
		String message = EarlyWarningService.edit(EarlyWarning);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 删除
	 * 
	 */
	@RequestMapping(value="/del",method={RequestMethod.POST})
	@ResponseBody
	public ResponseMessage del(Long id) {
		EarlyWarning EarlyWarning = new EarlyWarning();
		EarlyWarning.setId(id);
		String message = EarlyWarningService.del(EarlyWarning);
		return ResponseMessage.createMessage("ok".equals(message), message);
	}

	/**
	 * 查询所有预警
	 * 
	 */
	@RequestMapping(value="/list", method=RequestMethod.POST)
	@ResponseBody
	public List<EarlyWarning> listAll() {
		List<EarlyWarning> list = EarlyWarningService.findAll();
		return list;
	}
	
	/**
	 * 查询预警信息
	 */
	@RequestMapping(value="/info", method=RequestMethod.POST)
	@ResponseBody
	public EarlyWarning info(@RequestParam(value = "id", required = true) long id) {
		EarlyWarning EarlyWarning = EarlyWarningService.getById(id);
		return EarlyWarning;
	}
 
	/**
	 * 批量删除预警
	 */
	@RequestMapping(value="/delAll", method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage delAll(@RequestParam(value="id",required=true) String id) {
		String[] split = id.split(",");
		for (String str : split) {
			long parseLong = Long.parseLong(str);
			EarlyWarning EarlyWarning = EarlyWarningService.getById(parseLong);
			EarlyWarningService.del(EarlyWarning);
		}
		
		return ResponseMessage.createMessage(true, "ok");
	}
	
	
	/**
	 * 检测是否存在该预警
	 */
	@RequestMapping(value="/check", method=RequestMethod.POST)
	@ResponseBody
	public String check(@RequestParam(value="status",required=true) Integer status) {
		 
			EarlyWarning EarlyWarning = EarlyWarningService.findByStatus(status);
			 
	 if(StringUtils.isEmpty(EarlyWarning)) {
		 return "true";//可以添加
	 }else {
		 return "false";
	 }
	 
	}
	
	
	
	
	
	
	 
}
